import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        hasLogin: false,
        userName: "",
        socketTask: null,
        admin_info:{},
        admin_data:{},
        active_devices:{},
        groupSipVideo:{},
        sipObj:{}
    },
 
    mutations: {
        SET_SIPOBJ(state, sipObj){
            state.sipObj = sipObj
        },
        WEBSOCKET_INIT (state, url) {
            state.socketTask = uni.connectSocket({
                url,  // url是websocket连接ip
                success (res) {
                    console.log(res)
                    console.log('websocket连接成功')
                }
            })
            state.socketTask.onOpen((res) => {
                console.log('WebSocket连接正常打开中...！')
                // 注：只有连接正常打开中 ，才能正常收到消息
                state.socketTask.onMessage(res => {
                    let data  = JSON.parse(res.data)
                    if(data.cmd==='ma_login'){
                        console.log('第一次接收数据')
                        state.admin_info = data
                    }else if(data.cmd==='ma_get_active_devices'){
                        state.active_devices = data
                    }else if(data.cmd==='ma_open_group_sip_video_call'){
                        console.log('llllllllllllllllllllllllll')
                        state.groupSipVideo = data
                        console.log(data)
                    }else {
                        console.log(data)
                        console.log('mmmwebsoket数据')
                        state.admin_data={}
                        console.log('第二次接收数据')
                        state.admin_data = data
                    }
                });
            })
            state.socketTask.onError((res) => {
                console.log(125235)
                uni.showToast({
                    title: "服务器请求失败",
                    icon: "none",
                    duration: 2000
                  });
            })
        },
        WEBSOCKET_SEND (state, data) {
            state.socketTask.send({
               data,
               async success() {
                   console.log('消息发送成功')
               },
           })
        },
        CLOSE_SOCKET (state) {
            if (!state.socketTask) return
            state.socketTask.close({
                success (res) {
                    console.log('关闭成功', res)
                },
                fail (err) {
                    console.log('关闭失败', err)
                }
            })
        },
        SET_LOGIN(state, status) {
            state.hasLogin = status;
        },
        SET_USERNAME(state, name) {
            state.userName = name;
        },
    },
	actions: {
        sipObj({ commit }, sipObj) {                       
            commit('SET_SIPOBJ', sipObj)                   
          },
        WEBSOCKET_INIT({
            commit
                       }, url) {
            commit('WEBSOCKET_INIT', url)
        },
        WEBSOCKET_SEND({
            commit
                       }, data) {
            commit('WEBSOCKET_SEND', data)
        },
        CLOSE_SOCKET({
            commit
                     }) {
            commit('CLOSE_SOCKET')
        },
        SIP_OBJ({
            commit
                     }) {
            commit('SIP_OBJ')
        }
	}
})

export default store
